var appInstance = getApp();
var pageData    = {
  data: { 
    shade:true, 
    picker:{'li':[{'img':'../../images/delete.png','txt':'删除','eventParams':'delete',"eventHandler":"picker"},]}, 
    free_vessel4:{"content":[{"content":"http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg",'product_name':'公司名字','company_name':'',"eventParams":"a","eventHandler":"tapInnerLinkHandler",'status':false}],
    'pageNo':0}
  },
  onLoad: function (e) {
    var that =this;
    this.searchPrdList();
     },
  onShow: function(){ 
    var that = this;
    var content = that.data.free_vessel4.content;
    for(var i=0,len=content.length;i<len;i++){
      var _content = content[i];
       _content.status=false;
    }
    this.setData({
      free_vessel4:{
        content:content
      }
    })
  },
  navbarTap: function(e){
    var id = e.currentTarget.dataset.idx;
    var navbar = this.data.navbar;
    this.setData({  
      currentTab: id  
    })
  },
  searchPrdList:function(){
    var that = this;
    var pageNo = this.data.free_vessel4.pageNo;
    var param = appInstance.globalData.session_key;
    var searchPrdList ={
      token :param.myXcxSessionId,
      source:'1',  
      userId:param.userId,  
    }
    appInstance.sendRequest({
      url:'/cust/getMyCompany',
      data:searchPrdList,
      method:'POST',
      success: function(res){
        var data = res.userdata
        console.log(data);
        var arr = [];
        for(var i=0,len=data.length;i<len;i++){
          var dataset = {}
          dataset.product_name=data[i].companyName||'我的公司';dataset.content="http://img06.tooopen.com/images/20160818/tooopen_sy_175866434296.jpg";
          dataset.defaultSign=data[i].defaultSign;
          dataset.eventParams=data[i].companyId;
          dataset.eventHandler='tapInnerLinkHandler';
          dataset.status = false;
          arr.push(dataset);
        }
        var add = {"content":"http://oh4xneden.bkt.clouddn.com/tmp_1859413202o6zAJs1cHTCyKqdVf28s9WnUD35kb9246207723b9401fbc3c5f936488557.png",'product_name':'','company_name':'　',"eventParams":["add"],"eventHandler":"tapInnerLinkHandler"} ;
        arr.push(add);                   
        that.setData({
           free_vessel4:{"content":arr} 
        })
      },
      fail: function(res){
        console.log('getmode fail');
      },
      complete: function(res){
      }
    }) 
  },
  picker:function(e){
    let that = this;     
    var tapeven = e.currentTarget.dataset.eventParams;
    console.log(tapeven);
    var companyId='';
    var content = that.data.free_vessel4.content;
    for(var i=0,len=content.length;i<len;i++){
      var _content = content[i];
      if(_content.status==true){
        companyId=_content.eventParams
      }
    }
    if(companyId==''){
      appInstance.showModal({
        content:'请先选择公司'
      })
      return false;
    }
    var param = appInstance.globalData.session_key;
    var prod_param = {
        companyId:companyId.id,
        source:'1',
        token:param.myXcxSessionId,
        userId:param.userId
        } 
    if(tapeven=='delete'){
      that.setData({
        shade:true
      })
       appInstance.sendRequest({
              url:'/company/delCompany',
              data:prod_param,
              method:'POST',           
              success: function(res){
                if(res.result == 0){  
                  that.searchPrdList();
                  appInstance.showToast({
                    title: '删除成功！',
                    icon: 'success'
                  });           
                }else{
                  console.log(res.msg)
                  //提示无数据并navigateback
                }
              },
              fail: function(res){
                console.log('getmode fail');
              },
              complete: function(res){
              }
       },'https://bc.zhaojx.net/bizcat/webservice/rest') 
    }
  },
  pick_show:function(){
    var that =this;
    var boolean_set = !that.data.shade;
    console.log(boolean_set);
    //make it hide
     if(boolean_set==true){
      var arr = that.data.free_vessel4.content;
      for(var i=0,len=arr.length;i<len;i++){        
          arr[i].status=false;
      };
      var pageNo = this.data.free_vessel4.pageNo;
      that.setData({
        free_vessel4:{"content":arr}, 
        shade:boolean_set
       })
     }else{
      that.setData({
        shade:boolean_set
      })
     }
  },

  tapInnerLinkHandler:function(e){
    let that = this;    
      //长按待选项
    if(that.data.longtap == true){
      that.setData({
        shade:false,
        longtap:false
      })
      return false;
    }  
    var tapeven = e.currentTarget.dataset.eventParams;
    console.log(tapeven)
    if(that.data.shade == false){
      var arr = that.data.free_vessel4.content;
      for(var i=0,len=arr.length;i<len;i++){
        if(arr[i].eventParams == tapeven){         
            arr[i].status=!arr[i].status;
        }else{
          arr[i].status=false;
        }
      };
      var pageNo = this.data.free_vessel4.pageNo;
      that.setData({
        free_vessel4:{"content":arr,'pageNo':pageNo}, 
      })
    }else if(tapeven=='add'){
       that.setData({
        shade:true
      })
       wx.navigateTo({
            url: '../addcompany/addcompany?companyId=add',
            success: function(res){
              // success
            },
          })
    }else{
          var param = appInstance.globalData.session_key;
          var userId = param.userId;
          wx.navigateTo({
              url: '../companydetail/companydetail?userId='+userId+'&companyId='+tapeven,
            success: function(res){
              // success
          },
        })   
   }
  },
  //longtap  
 mytouchstart: function () {  
   let that = this;  
   that.setData({  
     longtap:true
   })  
 } 
  
};
Page(pageData);